package soa.security.web.action;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import soa.security.domain.Account;
import soa.security.domain.Applsystem;
import soa.security.domain.Person;
import soa.security.exception.OrganizationException;
import soa.security.mgr.service.RegisterMgrService;
import soa.security.web.support.action.LdapModelAction;
import v1.web.WebUtils;

import javax.annotation.Resource;

/**
 * @author CreGu
 * @date 2016/3/23
 */
public class RegisterAction extends LdapModelAction<Person> {
    protected static final Logger log = LoggerFactory.getLogger(RegisterAction.class);

    @Resource
    private RegisterMgrService registerMgrService;

    private Account account;
    private Person person;

    private String peid;
    private String orid;
    private String acid;
    private String bcflag;

    private String appkey;
    private String requestUri;
    private String bcymUrl;
    private String aat;

    @Override
    protected Person findByDn(String dn) {
        return null;
    }

    /**
     * 进入基本注册页面
     *
     * @return
     */
    public String register() {
        if (StringUtils.isBlank(requestUri))
            requestUri = "/";
        return SUCCESS;
    }

    /**
     * 基本注册提交
     *
     * @return
     */
    public String doneRegister() {
        Person pe = registerMgrService.addRegister(account, person);

        //todo test
//        Person pe = person;
//        pe.setUid("PE0134");
//        pe.setOrId("OR0002");
//        pe.setAccountId("test11@zj");

        boolean flag = (pe != null);

        JSONObject json = new JSONObject();
        json.put("success", flag);
        if (flag) {
            json.put("peid", pe.getUid());
            json.put("orid", pe.getOrId());
            json.put("appkey", appkey);
            json.put("requestUri", requestUri);
            json.put("accountUid", pe.getAccountId());
        }

        Applsystem applsystem = registerMgrService.bcReg(appkey, "0");
        if (applsystem != null) {
            json.put("bczc", true);

            json.put("bcymUrl", buildBycmUrl(applsystem.getBcymUrl(), pe.getAccountId()));
        } else {
            json.put("bczc", false);
        }


        return WebUtils.writeJson(ServletActionContext.getResponse(), json.toString());
    }

    public String between() {
        person = personMgrService.findByUid(peid, orid);
        return SUCCESS;
    }

    /**
     * 实名认证
     */
    public String certification() {
        person = personMgrService.findByUid(peid, orid);
        return SUCCESS;
    }

    public String doneCertification() {


        System.out.println(person.getIDNumber() + person.getSocialSecurityCard());
        Person person2 = personMgrService.findByUid(peid, orid);
        person2.setOrId(orid);

        boolean flag = registerMgrService.certification(person2, person);

        JSONObject json = new JSONObject();
        json.put("success", flag);
        if (flag) {
            json.put("peid", peid);
            json.put("orid", orid);
            json.put("appkey", appkey);
            json.put("requestUri", requestUri);
        }

        Applsystem applsystem = registerMgrService.bcReg(appkey, "1");
        if (applsystem != null) {
            json.put("bczc", true);
            json.put("accountUid", acid);
            json.put("bcymUrl", buildBycmUrl(applsystem.getBcymUrl(), acid));
        } else {
            json.put("bczc", false);
        }

        return WebUtils.writeJson(ServletActionContext.getResponse(), json.toString());
    }

    public String smbetween() {

        person = personMgrService.findByUid(peid, orid);
        return SUCCESS;
    }

    public String supplement() {

        return SUCCESS;
    }


    public String forw() {
        Account ac = accountMgrService.findByUid(acid);
        if (ac != null && ac.isReal()) {
            person = personMgrService.findByUid(peid, orid);
            return "subetween";
        }
        return "between";
    }

    public String accountConfirm() {
        JSONObject json = new JSONObject();
        boolean flag = true;
        try {
            flag = registerMgrService.accountConfirm(acid, aat);
        } catch (OrganizationException e) {
            e.printStackTrace();
            flag = false;
        }
        json.put("success", flag);
        return WebUtils.writeJson(ServletActionContext.getResponse(), json.toString());
    }

    public String goForward(){
        person = personMgrService.findByUid(peid, orid);
        return SUCCESS;
    }

    private String buildBycmUrl(String bcymUrl, String accountId) {
        if (StringUtils.contains(bcymUrl, "?")) {
            if (StringUtils.endsWith(bcymUrl, "?") || StringUtils.endsWith(bcymUrl, "&")) {
                return bcymUrl + "accountUid=" + accountId;
            }
            return bcymUrl + "&accountUid=" + accountId;
        } else {
            return bcymUrl + "?accountUid=" + accountId;
        }

    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public String getPeid() {
        return peid;
    }

    public void setPeid(String peid) {
        this.peid = peid;
    }

    public String getOrid() {
        return orid;
    }

    public void setOrid(String orid) {
        this.orid = orid;
    }

    public String getAppkey() {
        return appkey;
    }

    public void setAppkey(String appkey) {
        this.appkey = appkey;
    }

    public String getRequestUri() {
        return requestUri;
    }

    public void setRequestUri(String requestUri) {
        this.requestUri = requestUri;
    }

    public String getAcid() {
        return acid;
    }

    public void setAcid(String acid) {
        this.acid = acid;
    }

    public String getBcymUrl() {
        return bcymUrl;
    }

    public void setBcymUrl(String bcymUrl) {
        this.bcymUrl = bcymUrl;
    }

    public String getBcflag() {
        return bcflag;
    }

    public void setBcflag(String bcflag) {
        this.bcflag = bcflag;
    }

    public String getAat() {
        return aat;
    }

    public void setAat(String aat) {
        this.aat = aat;
    }
}
